package com.mine.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.mine.controller.StaticUtil;

import com.mine.model.Game;
import com.mine.controller.GameControl;
import com.mine.controller.GameResource;

/**
 * 游戏主窗口
 * 
 */
public class MainWindow extends JFrame  {
	private static final ImageIcon ICON_APP= GameResource.getResource(StaticUtil.Icon.APP);
	
	public GameMenuBar menuBar;
	public GameControlPanel controlPanel;
	public GameMinePanel minePanel;
	private volatile static MainWindow mainWindow;//内存可见性

	/**
	 * 单例模式 懒汉式，线程安全
	 * 使用双重检查锁+内存可见性 解决多线程问题 提升效率
	 * @return MainWindow对象
	 */
	public static MainWindow getMainWindowInstance(GameControl model){
		if(mainWindow==null){
			synchronized(MainWindow.class){
				if(mainWindow==null){
					mainWindow=new MainWindow(model);
				}
			}
		}
		return mainWindow;
	}

	private MainWindow(GameControl model) {
		this.setTitle("扫雷");
		this.setIconImage(ICON_APP.getImage());
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.initContentPanel();
		// init menuBar
		this.menuBar = new GameMenuBar(model);
		this.setJMenuBar(this.menuBar);
		// init control panel
		this.controlPanel = new GameControlPanel(model);
		this.add(controlPanel, BorderLayout.NORTH);
		// init mine panel
		this.minePanel = new GameMinePanel(model);
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				Game.dispatchEvent("exitGame");
			}
		});
		this.add(minePanel);
	}
	private void initContentPanel(){
		JPanel contentPanel = new JPanel(new BorderLayout());
		contentPanel.setBackground(new Color(0xc0c0c0));
		contentPanel.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createMatteBorder(2, 2, 0, 0, Color.WHITE),
				BorderFactory.createEmptyBorder(6, 6, 6, 6)));
		this.setContentPane(contentPanel);
	}
	
	
}
